﻿package mortal.game.view.achievement.panel
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.view.achievement.data.*;
    import mortal.game.view.achievement.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;

    public class AchievementResultPanel extends GSprite
    {
        private var _bg:GImageBitmap;
        private var _littleCellArr:Array;
        private var _pageSelector:PageSelecter;
        private var _pageTool:PageTool;
        private var _category:int;

        public function AchievementResultPanel()
        {
            this._pageTool = new PageTool([], 4);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.AchievementCurResultBg, 0, 0, this);
            this._pageSelector = UIFactory.pageSelecter(255, 475, this, PageSelecter.CompleteMode);
            this._pageSelector.pageTextBoxSize = 50;
            this._pageSelector.configEventListener(Event.CHANGE, this.onCellChange);
            this.genCells(null);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            var _loc_3:AchievementResultCell = null;
            super.disposeImpl(param1);
            this._bg.dispose(param1);
            this._bg = null;
            this._pageSelector.dispose(param1);
            this._pageSelector = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._littleCellArr.length)
            {
                
                _loc_3 = this._littleCellArr[_loc_2];
                _loc_3.dispose(param1);
                _loc_2++;
            }
            this._littleCellArr = null;
            return;
        }// end function

        private function genCells(param1:Array) : void
        {
            var _loc_8:int = 0;
            var _loc_9:AchievementResultCell = null;
            var _loc_2:int = 2;
            var _loc_3:int = 4;
            var _loc_4:int = 5;
            var _loc_5:int = 5;
            var _loc_6:int = 310;
            var _loc_7:int = 230;
            this._littleCellArr = [];
            _loc_8 = 0;
            while (_loc_8 < _loc_3)
            {
                
                _loc_9 = UICompomentPool.getUICompoment(AchievementResultCell) as AchievementResultCell;
                _loc_9.x = _loc_4 + _loc_6 * int(_loc_8 % _loc_2);
                _loc_9.y = _loc_5 + _loc_7 * int(_loc_8 / _loc_2);
                this.addChild(_loc_9);
                this._littleCellArr.push(_loc_9);
                _loc_8++;
            }
            return;
        }// end function

        public function updateByTabIndex(param1:int, param2:Boolean = true) : void
        {
            this._category = AchievementUtil.tabIndexToCategory(param1);
            if (this._category == 0)
            {
                return;
            }
            var _loc_3:* = Cache.instance.achievent.catoryTypeObj[this._category];
            if (_loc_3 == null)
            {
                _loc_3 = [];
            }
            this._pageTool.array = _loc_3;
            if (param2)
            {
                this._pageSelector.currentPage = 1;
                this._pageSelector.maxPage = Math.ceil(_loc_3.length / 4);
            }
            this.onCellChange(null);
            return;
        }// end function

        public function flyTypeIcon() : Boolean
        {
            var _loc_3:AchievementResultCell = null;
            var _loc_1:* = Cache.instance.achievent.getTypeType;
            Cache.instance.achievent.getTypeType = -1;
            if (_loc_1 == -1 || this._littleCellArr == null)
            {
                return false;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._littleCellArr.length)
            {
                
                _loc_3 = this._littleCellArr[_loc_2];
                if (_loc_3.flyIcon(_loc_1))
                {
                    return true;
                }
                _loc_2++;
            }
            return false;
        }// end function

        public function turnToPageByType(param1:int, param2:int) : void
        {
            var _loc_5:SAchievementType = null;
            var _loc_6:int = 0;
            this._category = AchievementUtil.tabIndexToCategory(param1);
            if (this._category == 0)
            {
                return;
            }
            var _loc_3:* = Cache.instance.achievent.catoryTypeObj[this._category];
            if (_loc_3 == null)
            {
                _loc_3 = [];
            }
            this._pageTool.array = _loc_3;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3.length)
            {
                
                _loc_5 = _loc_3[_loc_4];
                if (_loc_5.type == param2)
                {
                    _loc_6 = int(_loc_4 / 4) + 1;
                    this._pageSelector.currentPage = _loc_6;
                    this._pageSelector.maxPage = Math.ceil(_loc_3.length / 4);
                    break;
                }
                _loc_4++;
            }
            this.onCellChange(null);
            return;
        }// end function

        private function onCellChange(event:Event) : void
        {
            var _loc_3:int = 0;
            var _loc_4:AchievementResultCell = null;
            var _loc_5:SAchievementType = null;
            var _loc_6:AchievementResultCell = null;
            this.clearData();
            var _loc_2:* = this._pageTool.getByPageNo(this._pageSelector.currentPage);
            _loc_3 = 0;
            while (_loc_3 < _loc_2.length && this._littleCellArr.length)
            {
                
                _loc_4 = this._littleCellArr[_loc_3];
                _loc_5 = _loc_2[_loc_3];
                _loc_4.updateTypeCell(_loc_5);
                _loc_4.showHide(false);
                this.addChild(_loc_4);
                _loc_3++;
            }
            if (AchievementUtil.HideCategory == this._category)
            {
                while (_loc_3 < this._littleCellArr.length)
                {
                    
                    _loc_6 = this._littleCellArr[_loc_3];
                    _loc_6.showHide(true);
                    this.addChild(_loc_6);
                    _loc_3++;
                }
            }
            return;
        }// end function

        private function clearData() : void
        {
            var _loc_2:AchievementResultCell = null;
            if (this._littleCellArr == null)
            {
                return;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._littleCellArr.length)
            {
                
                _loc_2 = this._littleCellArr[_loc_1];
                if (this.contains(_loc_2))
                {
                    this.removeChild(_loc_2);
                }
                _loc_1++;
            }
            return;
        }// end function

    }
}
